package com.wwt.springboot_vue.controller;



import com.wwt.springboot_vue.bean.Notice;
import com.wwt.springboot_vue.mapper.NoticeMapper;
import com.wwt.springboot_vue.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeService noticeService;

    @PostMapping
    public  Integer save(@RequestBody Notice notice){
        System.out.println(notice.toString());
        //新增  更新
      return   noticeService.save(notice);

    }

    //查询所有
    @GetMapping("/findAll")
    public List<Notice> findALL() {
        List<Notice> all = noticeMapper.findAll();
        return all;
    }

    @DeleteMapping("/{notice_id}")
    public  Integer delete(@PathVariable Integer notice_id){

       return noticeMapper.deleteById(notice_id);
    }

    //分页查询
    //接口路径  /notice/page?pageNum=1&pageSize=10
    //@RequestParam接收
    //mysql查询分页 limit 第一个参数  (pageNum - 1) * pageSize  第二个不变 pageSize

    @GetMapping("/page")
    public Map<String ,Object> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize ,@RequestParam String notice_title) {

        pageNum = (pageNum - 1) * pageSize;
        List<Notice> data =  noticeMapper.selectPage(pageNum,pageSize,notice_title);
         Integer total =  noticeMapper.selectTotal(notice_title);
        Map<String ,Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

}
